﻿using Rougamo;
using Rougamo.Context;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace RougamoService.Model
{
    // 1.NuGet引用Rougamo.Fody
    // 2.定义类继承MoAttribute，同时定义需要织入的代码
    public class LoggingAttribute : MoAttribute
    {
        public override void OnEntry(MethodContext context)
        {
            // 从context对象中能取到包括入参、类实例、方法描述等信息
            Console.WriteLine($"方法执行前");
        }

        public override void OnException(MethodContext context)
        {
            Console.WriteLine($"方法执行异常{context.Exception.Message}");
        }

        public override void OnSuccess(MethodContext context)
        {
            Console.WriteLine($"方法执行成功后");
        }

        public override void OnExit(MethodContext context)
        {
            Console.WriteLine($"方法退出时，不论方法执行成功还是异常，都会执行");
        }
    }

    // 3.应用Attribute
    public class LogService
    {
        [Logging]
        public static int Sync(string model)
        {
            return 1;
        }

        [Logging]
        public async Task<int> Async(int id)
        {
            return id + 1;
        }
    }
}
